/**
 * Created by shidoudou on 14-5-15.
 * 获取活动目录数据
 *看哪些活动
 */
var mongodb = require('./db');

function Project (project){
    this.name = project.name;
    this.describe = project.describe
}

module.exports = Project;

Project.prototype.save = function(callback){
    console.log(this.name);

    var project = {
        name: this.name,
        describe: this.describe
    }


    //  open database
    mongodb.open(
        function(err, db){
            if(err){
                return callback(err);
            }
            db.collection('projects', function(err, collection){
                if(err){
                    mongodb.close();
                    return callback(err);
                }

                //insert data
                collection.insert(project, {
                    safe: true
                }, function(err, project){
                    mongodb.close();
                    if(err){
                        return callback(err);
                    }
                    callback(null, project[0]);
                })
            });
        }
    );
}


Project.get = function(project, callback){
    mongodb.open(function(err, db){
        if(err){
            return callback(err);
        }
        db.collection('projects', function(err, collection){
            if(err){
                mongodb.close();
                return callback(err);
            }
            var query ={};
            if(project){
                query = project;
            }
            collection.find(query).toArray(function(err, docs){
                mongodb.close();
                if(err){
                    return callback(err);
                }
                callback(null, docs);
            });
        });
    });
}